﻿//using SlaughterItem.Common.Dtos;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WinFormsApp1.Common;
using static WinFormsApp1.Common.HttpClientHelper;
using static WinFormsApp1.TongShiWei.ShowScaleContext;

namespace WinFormsApp1.TongShiWei
{
    public partial class ShowScaleContext : UIForm
    {
        public ShowScaleContext()
        {
            InitializeComponent();
            //获取磅秤表数据
            GetScaleData();
        }
        int AddNum = 0;
        public async void GetScaleData()
        {
            GetScaleContext getScaleContext = new GetScaleContext();
            getScaleContext.ScaleName = ScaleDataName_.Text;
            // http://localhost:5159/api/TSW/GetScaleData
            var res = await HttpClientHelper.RequestUrl<GetScaleContext, ApiResult<List<ShowScaleDataDTO>>>(URLCommon.Url2, "/TSW/GetScaleData", HttpClientHelper.MethodType.POST, getScaleContext);
            if (res.Code > 0)
            {
                foreach (var item in res.Data)
                {
                    ScaleListData_.Rows.Add(item.ScaleId, item.ScaleName, item.ScaleTypeName, item.TareNum, item.IsTareAdd == true ? "禁用" : "启用", item.DepotTypeName, item.IsGoDepot == true ? "禁用" : "启动", item.OperatorName, item.IsScaleDisable == true ? "禁用" : "启动", item.OperatorName, item.ScaleCreateTime.ToString("yyyy-MM-dd"), item.Modifier, item.RevempTime.ToString("yyyy-MM-dd"));
                }
                if (AddNum == 0)
                {
                    //禁用
                    DataGridViewButtonColumn ForbidButton = new DataGridViewButtonColumn();
                    ForbidButton.Text = "禁用";
                    ForbidButton.UseColumnTextForButtonValue = true;
                    //修改
                    DataGridViewButtonColumn UpdateButton = new DataGridViewButtonColumn();
                    UpdateButton.Text = "修改";
                    UpdateButton.UseColumnTextForButtonValue = true;
                    //启动 
                    DataGridViewButtonColumn DeleteButton = new DataGridViewButtonColumn();
                    DeleteButton.Text = "删除";
                    DeleteButton.UseColumnTextForButtonValue = true;
                    ScaleListData_.Columns.AddRange(ForbidButton, UpdateButton, DeleteButton);
                    AddNum += 1;
                    ScaleListData_.CellClick += new DataGridViewCellEventHandler(ScaleListData_CellContentClick);
                }
                //ScaleListData_.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "禁用";
                if (Rows >= 0 && ColNum > 0)
                {
                    if (ScaleTypeButton == 1)
                    {
                        ScaleListData_.Rows[Rows].Cells[ColNum].Value = "启用";
                    }
                    else if (ScaleTypeButton == 2)
                    {
                        ScaleListData_.Rows[Rows].Cells[ColNum].Value = "禁用";
                    }
                    Rows = 0;
                    ColNum = 0;
                }
            }
        }
        public class ShowScaleDataDTO
        {
            /// <summary>
            /// 磅秤表ID
            /// </summary>
            public int ScaleId { get; set; }
            /// <summary>
            /// 磅秤类型ID
            /// </summary>
            public int ScaleTypeId { get; set; }
            /// <summary>
            /// 磅秤名称
            /// </summary>
            public string ScaleName { get; set; }
            /// <summary>
            /// 使用状态
            /// </summary>
            public bool IsScaleState { get; set; }
            /// <summary>
            /// 皮重扣减(公斤)
            /// </summary>
            public int TareNum { get; set; }
            /// <summary>
            /// 皮重按件累加
            /// </summary>
            public bool IsTareAdd { get; set; }
            /// <summary>
            /// 绑定仓库
            /// </summary>
            public int BindDepotId { get; set; }
            /// <summary>
            /// 自动出/入库
            /// </summary>
            public bool IsGoDepot { get; set; }
            /// <summary>
            /// 商品代号名称
            /// </summary>
            public string UPCDataName { get; set; }
            /// <summary>
            /// 操作人
            /// </summary>
            public string OperatorName { get; set; }
            /// <summary>
            /// 创建时间
            /// </summary>
            public DateTime ScaleCreateTime { get; set; }
            /// <summary>
            /// 最后修改人
            /// </summary>
            public string Modifier { get; set; }
            /// <summary>
            /// 最后修改时间
            /// </summary>
            public DateTime RevempTime { get; set; }
            /// <summary>
            /// 是否删除{false:不删除,true:删除}
            /// </summary>
            public bool IsScaleDelete { get; set; }
            /// <summary>
            /// 是否禁用{false:不禁用,true:禁用}
            /// </summary>
            public bool IsScaleDisable { get; set; }
            /// <summary>
            /// 磅秤类型名称
            /// </summary>
            public string ScaleTypeName { get; set; }
            /// <summary>
            /// 仓库类型名称
            /// </summary>
            public string DepotTypeName { get; set; }
        }
        public class GetScaleContext
        {
            public string ScaleName { get; set; }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            var RowsCount = ScaleListData_.Rows.Count - 1;
            for (int i = 1; i <= RowsCount; i++)
            {
                ScaleListData_.Rows.RemoveAt(0);
            }
            GetScaleData();
        }

        private void uiButton5_Click(object sender, EventArgs e)
        {
            AddScaleContext addScaleContext = new AddScaleContext();
            this.Hide();
            addScaleContext.Show();
        }

        private void uiPanel4_Click(object sender, EventArgs e)
        {
            uiButton5_Click(sender, e);
        }
        #region  禁用和删除磅秤表数据
        /// <summary>
        /// 禁用和删除磅秤表数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 刷新页面
        int UPdataNum = 0;
        //记录修改的行数
        int Rows = 0;
        //记录修改的列数
        int ColNum = 0;
        //区分按钮名称
        int ScaleTypeButton = 0;
        private async void ScaleListData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            //http://localhost:5159/api/TSW/DelUpdateScaleData
            if (!(e.ColumnIndex >= 13))
            {
                return;
            }
            else
            {
                ScaleDelDisable data = new ScaleDelDisable();
                var ScaleRowData = ScaleListData_.Rows[e.RowIndex].Cells[0].Value;
                data.Pid = e.ColumnIndex;
                data.ScaleId = Convert.ToInt32(ScaleRowData);
                switch (e.ColumnIndex)
                {
                    case (13 or 15):
                        if (e.ColumnIndex == 13)
                        {
                            var res = await HttpClientHelper.RequestUrl<ScaleDelDisable, ApiResult<ScaleDelDisable>>(URLCommon.Url1, "/TSW/DelUpdateScaleData", MethodType.POST, data);
                            if (e.ColumnIndex>0 && e.RowIndex>=0 && res.Code > 0)
                            {
                                var ScaleState = ScaleListData_.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                                if (ScaleState == null)
                                {
                                    return;
                                }
                                else
                                {
                                    Rows = e.RowIndex;
                                    ColNum = e.ColumnIndex;
                                    UPdataNum = 1;
                                    if ((ScaleState).ToString() == "禁用")
                                    {
                                        ScaleTypeButton = 1;
                                        MessageBox.Show("启用成功");
                                    }
                                    else
                                    {
                                        ScaleTypeButton = 2;
                                        MessageBox.Show("禁用成功");
                                    }
                                }
                            }
                        }
                        else if (e.ColumnIndex == 15)
                        {
                            var ScaleState = ScaleListData_.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                            if (ScaleState == null)
                            {
                                return;
                            }
                            var res_ = await HttpClientHelper.RequestUrl<ScaleDelDisable, ApiResult<ScaleDelDisable>>("http://localhost:5159/api/TSW/", "DelUpdateScaleData", MethodType.POST, data);
                            if (res_.Code > 0)
                            {
                                if (UPdataNum > 0)
                                {
                                    return;
                                }
                                UPdataNum = 1;
                                MessageBox.Show("删除成功");
                            }
                            else
                            {
                                MessageBox.Show("删除成功");
                            }
                        }
                        break;
                    case 14:
                     //AddScaleContext addScaleContext = new AddScaleContext(data.ScaleId);
                     //   addScaleContext.Show();
                        break;
                }
            }
            if (UPdataNum>0) 
            {
                for (int i = (ScaleListData_.Rows.Count - 1); i > 0; i--)
                {
                    ScaleListData_.Rows.RemoveAt(i - 1);
                }
                UPdataNum = 0;
                GetScaleData();
            }
        }
        public class ScaleDelDisable
        { 
            public int ScaleId {  get; set; }
            public int Pid {  get; set; }
        }
        #endregion
    }
}
